<?php

namespace app\Services\api;


use yii;
use app\models\Room;
use app\models\Steaming;
use app\Services\ApiService;
use yii\db\Expression;


class RoomService extends ApiService
{

    /**
     * @return Service_Pay
     */
    public static function getInstance()
    {
        return parent::getInstance();
    }
    /**
     * 获取已开放房间基本信息
     *
     */
    public function getList()
    {
        $data = Room::find()
            ->select(['ID', 'name', 'maxseat', 'curseat', 'Activity', 'opentime'])
            ->asArray()->all();
        if (empty($data)) {
            return ['code' => -1, 'message' => 'no room'];
        }
        return ['code' => 200, 'data' => $data];
    }

    /**
     *  获取主持人开关信息
     * @param [type] $room_id
     */
    public function getRoomHost($room_id)
    {
        // 获取房间信息
        $roomInfo =  Room::find()->where(['ID' => $room_id])->asArray()->one();
        if (empty($roomInfo)) {
            return ['code' => -1, 'message' => 'no room'];
        }
        return ['code' => 200, 'data' => $roomInfo['host']];
    }

    /**
     * 加入房间
     */
    public function joinRoom($room_id)
    {
        // 获取房间信息
        $roomInfo =  Room::find()->where(['ID' => $room_id])->asArray()->one();
        if (empty($roomInfo)) {
            return ['code' => -1, 'message' => 'no room'];
        }
        if ($roomInfo['curseat'] + 1 > $roomInfo['maxseat']) {
            return ['code' => -1, 'message' => 'roomful'];
        }
        // if($roomInfo['opentime'] < date('Y-m-d H:i:s',time())){
        //     return ['code' => -1, 'message' => '未到房间开启时间'];
        // }
        $res = Yii::$app->db->createCommand()->update('`pltf.room`', [
            'curseat' => new Expression('curseat+1'),
        ], ['ID' => $room_id])->execute();
        if ($res) {
            return ['code' => 200, 'message' => '加入房间成功'];
        }
        return ['code' => -1, 'message' => '加入房间失败'];
    }


    /**
     * 退出房间
     */
    public function quitRoom($room_id)
    {
        // 获取房间信息
        $roomInfo =  Room::find()->where(['ID' => $room_id])->asArray()->one();
        if (empty($roomInfo)) {
            return ['code' => -1, 'message' => 'no room'];
        }
        if ($roomInfo['curseat'] < 1) {
            return ['code' => -1, 'message' => 'empty room'];
        }
        $res = Yii::$app->db->createCommand()->update('`pltf.room`', [
            'curseat' => new Expression('curseat-1'),
        ], ['ID' => $room_id])->execute();
        if ($res) {
            return ['code' => 200, 'message' => '退出房间成功'];
        }
        return ['code' => -1, 'message' => '退出房间失败'];
    }


    /**
     * 房间用户人数
     */
    public function getRoomCount()
    {
        # 用户人数
        $room_list = Room::find()->where(['Activity' => 1])
            ->select('ID,name,curseat,maxseat,normal,reporter,VIP,opentime')
            ->asArray()
            ->all();
        $str = '';
        if (!empty($room_list)) {
            foreach ($room_list as $room) {
                $str .= $room['ID'] . '/' . $room['name'] . '/' . $room['curseat'] . '/' . $room['maxseat']  . '/' . $room['normal']  . '/' . $room['reporter']  . '/' . $room['VIP'] . '/' . $room['opentime'] . "</br>";
            }
            $str = substr($str, 0, strlen($str) - 5);
            return ['code' => 200, 'data' => $str];
        }
        return ['code' => 200, 'data' => [], 'message' => '未查询到数据'];
    }


    /**
     * 获取节目列表
     */
    public function getProgramList($req)
    {
        if (!empty($req['type'])) {
            $list = Room::find()
                ->alias('room')
                ->where(['Activity' => 1])
                ->select('ID,room.name')
                ->asArray()
                ->all();
            return  ['code' => 200, 'data' => $list];
        }
        $list = Room::find()
            ->alias('room')
            ->where(['Activity' => 1])
            ->select('ID,room.reporter,room.name,room.opentime')
            ->asArray()
            ->all();
        foreach ($list as &$room) {
            $info = Steaming::find()
                ->select('steamURL')
                ->where(['roomID' => $room['ID']])
                ->asArray()
                ->one();
            $room['steamURL'] = $info['steamURL'];
        }
        return  ['code' => 200, 'data' => $list];
    }

    /**
     * 保存房间设置
     */
    public function saveProgram($req)
    {
        $room = Room::find()
            ->where(['ID' => $req['ID']])
            ->asArray()
            ->one();
        if (empty($room)) {
            return  ['code' => -1, 'message' => '节目编号错误'];
        }
        $info['reporter'] = intval($req['reporter']);
        if (!empty($info['opentime'])) {
            $info['opentime'] = $req['opentime'];
        }
        $res = Yii::$app->db->createCommand()->update('`pltf.room`', $info, ['ID' => intval($req['ID'])])->execute();
        if (!empty($req['steamURL'])) {
            $st = Steaming::find()
                ->where(['roomID' => $req['ID']])
                ->asArray()
                ->one();
            if (!empty($st)) {
                Yii::$app->db->createCommand()->update('`pltf.steaming`', ['steamURL' => $req['steamURL']], ['roomID' => intval($req['ID'])])->execute();
            } else {
                $insert = [
                    'steamURL' => $req['steamURL'],
                    'roomID'  => $req['ID']
                ];
                Yii::$app->db->createCommand()->insert('`pltf.steaming`', $insert, ['roomID' => intval($req['ID'])])->execute();
            }
        }
        if ($res) {
            return  ['code' => 200, 'message' => '保存成功'];
        }
        return  ['code' => -1, 'message' => '保存失败'];
    }
}
